package com.hd.assistant.dal.dao.ibatis.ordermining;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hd.assistant.dal.dao.exception.DAOException;
import com.hd.assistant.dal.dao.orderming.NotifyMessageDAO;
import com.hd.assistant.dal.dao.query.ordermining.NotifyMessageQuery;
import com.hd.assistant.dal.dataobject.ordermining.NotifyMessage;
import com.taobao.common.dao.persistence.DBRoute;
import com.taobao.common.dao.persistence.SqlMapBaseDAO;

public class IbatisNotifyMessageDao extends SqlMapBaseDAO implements NotifyMessageDAO{

	@Override
	public void insertNotifyMessage(NotifyMessage message) throws DAOException {
		if(message == null)
			throw new DAOException("ERROR PARAMS: message == nul");
	try {
		this.executeInsert("NotifyMessage.insertNotifyMessageDO", message,DBRoute.getOrderMiningRoute());
	} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
		throw new DAOException("insertNotifyMessage", e);
	}
		
	}

	@Override
	public int updateMessageStatus(Long id, Integer status) throws DAOException {
		if(id == null || status == null)
			throw new DAOException("ERROR PARAMS: id == null || status == nul");
		NotifyMessageQuery query = new NotifyMessageQuery();
		query.setId(id);
		query.setStatus(status);
		try {
			return this.executeUpdate("NotifyMessage.updateMessagesStatus", query,DBRoute.getOrderMiningRoute());
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("updateMessageStatus id = "+id, e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<NotifyMessage> getMessageByLimit(Long fromNotInclude,
			Integer size) throws DAOException {
		if(fromNotInclude == null || size == null)
			throw new DAOException("ERROR PARAMS: fromNotInclude == null || size == null");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("idFrom", fromNotInclude);
		params.put("limitSize", size);
		
		try {
			return (List<NotifyMessage>)this.executeQueryForList("NotifyMessage.getMessagesByLimit", params, DBRoute.getOrderMiningRoute());
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("getMessageByLimit fromNotInclude = "+fromNotInclude, e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public NotifyMessageQuery getMessageByQuery(NotifyMessageQuery query)
			throws DAOException {
		if(query == null )
			throw new DAOException("ERROR PARAMS: query == null");
		try {
			
			Long count = (Long)this.executeQueryForObject("NotifyMessage.getCountByQuery", query,  DBRoute.getOrderMiningRoute());
			query.setTotalCount(count.intValue());
			query.setMessages((List<NotifyMessage>)this.executeQueryForList("NotifyMessage.getMessagesByQuery", query, DBRoute.getOrderMiningRoute()));
			 return query;
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("getMessageByQuery ", e);
		}
		
		
	}

	@Override
	public Long getLastProcessedId() throws DAOException {
		try {
			return (Long)this.executeQueryForObject("NotifyMessage.getLastProcessId", null, DBRoute.getOrderMiningRoute());
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("getLastProcessedId ", e);
		}
	}

	@Override
	public int updateLastProcessedId(Long lastProcessId) throws DAOException {
		if(lastProcessId == null )
			throw new DAOException("ERROR PARAMS: lastProcessId == null");
		try {
			return this.executeUpdate("NotifyMessage.updateLastProcessId", lastProcessId, DBRoute.getOrderMiningRoute());
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("updateLastProcessedId lastProcessId= "+lastProcessId, e);
		}
		
	}

	@Override
	public boolean lock(String hostId, Integer timeoutSec) throws DAOException {
		if(hostId == null )
			throw new DAOException("ERROR PARAMS: hostId == null");
		if(timeoutSec == null)
			timeoutSec = 600;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hostId", hostId);
		params.put("timeoutSec", timeoutSec);
		int count = 0;
		try {
			count =  this.executeUpdate("NotifyMessage.lockMessageslock", params, DBRoute.getOrderMiningRoute());
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			count = 0;
		}
		return count == 1;
	}

	@Override
	public boolean unLock(String hostId) throws DAOException {
		if(hostId == null )
			throw new DAOException("ERROR PARAMS: hostId == null");
		
		int count = 0;
		try {
			count =  this.executeUpdate("NotifyMessage.unlockMessageslock", hostId, DBRoute.getOrderMiningRoute());
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			count = 0;
		}
		return count == 1;
	}


	

}
